package com.webproject.shop.controller;

import com.webproject.shop.common.BusinessException;
import com.webproject.shop.common.ErrorCode;
import com.webproject.shop.common.ResponseModel;
import com.webproject.shop.common.ToolBox;
import com.webproject.shop.pojo.Admin;
import com.webproject.shop.pojo.Product;
import com.webproject.shop.pojo.User;
import com.webproject.shop.service.AdminService;
import com.webproject.shop.service.HistoryService;
import com.webproject.shop.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
@CrossOrigin(origins = "${webproject.web.path}", allowedHeaders = "*", allowCredentials = "true")
public class AdminController implements ErrorCode {
    @Autowired
    private AdminService adminService;

    @Autowired
    private ProductService productService;

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel login(String username, String password, HttpSession session) {
        if (StringUtils.isEmpty(username)
                || StringUtils.isEmpty(password)) {
            throw new BusinessException(PARAMETER_ERROR , "参数不合法！");
        }
        String md5pwd = ToolBox.md5(password);
        Admin admin = adminService.login(username, md5pwd);
        session.setAttribute("loginAdmin", admin);
        return new ResponseModel(admin);
    }

    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getProductList() {
        List<Product> products = productService.findAll();
        return new ResponseModel(products);
    }

    @RequestMapping(path = "/addproduct", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel addProduct(Product product){
        adminService.addProduct(product);
        return new ResponseModel();
    }



    @RequestMapping(path = "/deleteproduct", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel deleteProduct(int id){  //ajax可能不能传整数，暂时注意这个位置
        adminService.deleteProduct(id);
        return new ResponseModel();
    }

    @RequestMapping(path = "/updateproduct", method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel updateProduct(Product product){
        adminService.updateProduct(product);
        return new ResponseModel();
    }

    @RequestMapping(path = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel logout(HttpSession session,String token) {
        session.invalidate();
        return new ResponseModel();
    }

    @RequestMapping(path = "/status", method = RequestMethod.GET)
    @ResponseBody
    public ResponseModel getUser(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("loginAdmin");
        return new ResponseModel(admin);
    }

}
